/**
 * 
 */
package DB;

import gui.LoaderGif;

import java.util.List;

import main.Session;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.widgets.*;
import org.eclipse.wb.swt.SWTResourceManager;

import Utils.Concurrent.ThreadTasks.SearchMediaTask;
import Utils.Concurrent.ThreadTasks.SearchPersonTask;
import Utils.Concurrent.ThreadTasks.UpdateMediaTask;

import datatype.DBtypes.FilmfromDB;
import datatype.searchtype.SearchResult.MediaRes;
import datatype.searchtype.SearchResult.PersonRes;

/**
 * @author Yuval
 * @category the class handle the edit film composite prepare all the widgets.
 *           with this class the admin user can create or edit existing movie,
 *           add actors, and directors, sequel and prequal mpvies and etc
 * 
 * 
 */
public class EditFilm {

	private Text filmName, country, prequel, sequel, characterName;
	private Spinner release;
	private Spinner value;
	private Table actorTBL, directorTBL;
	private Button save, cencel, searchActor, searchDirector, setCharacterName;
	private Composite searchActorComp, searchDirectorComp, searchFilmComp,
			searchComp;
	private Canvas loadingCNVS;

	private FilmfromDB film = null;
	private List<PersonRes> ActorRes = null;
	private List<PersonRes> DirectorRes = null;
	private List<MediaRes> FilmsRes = null;
	private boolean isPrequel = true;

	private boolean isActorCompOn = false, isDirectorCompOn = false,
			isFilmCompOn = false;
	private SearchPersonTask searchActorTask = null;
	private SearchPersonTask searchDirectorTask = null;
	private SearchMediaTask searchFilmTask = null;

	// the search actor, director & film widgets

	private Text editFilm_searchActorTXT;
	private Table editFilm_searchActorTBL;
	private Button editFilm_searchActorGoBTN;
	private Button editFilm_searchActorAddBTN;
	private Button editFilm_searchActorCencelBTN;
	private Text editFilm_searchActorCharaterNameTXT;
	private Text editFilm_searchDirectorTXT;
	private Button editFilm_searchDirectorGoBTN;
	private Table editFilm_searchDirectorTBL;
	private Button editFilm_searchDirectorCencelBTN;
	private Button editFilm_searchDirectorAddBTN;
	private Text searchFilmTXT;
	private Button searchFilm_GoBTN;
	private Table searchFilm_filmTBL;
	private Button searchFilm_cencelBTN;
	private Button searchFilm_addBTN;
	private Canvas editFilm_searchActorLoadingCNVS;
	private Canvas editFilm_searchDirectorLoadingCNVS;
	private Canvas editFilm_searchFilmLoadingCNVS;

	public EditFilm(Text filmName, Text country, Text prequel, Text sequel,
			Text characterName, Spinner release, Spinner value, Table actorTBL,
			Table directorTBL, Button save, Button cencel, Button searchActor,
			Button setCharacterName, Button searchDirector,
			Composite searchActorComp, Composite searchDirectorComp,
			Composite searchFilmComp, Composite searchComp, Canvas loadingCNVS) {
		super();
		this.filmName = filmName;
		this.country = country;
		this.prequel = prequel;
		this.sequel = sequel;
		this.release = release;
		this.value = value;
		this.actorTBL = actorTBL;
		this.directorTBL = directorTBL;
		this.save = save;
		this.cencel = cencel;
		this.searchActor = searchActor;
		this.searchDirector = searchDirector;
		this.searchActorComp = searchActorComp;
		this.searchDirectorComp = searchDirectorComp;
		this.searchFilmComp = searchFilmComp;
		this.searchComp = searchComp;
		this.loadingCNVS = loadingCNVS;
		this.setCharacterName = setCharacterName;
		this.characterName = characterName;

		prepareSearch();
		prepareWidgets();

	}

	public synchronized FilmfromDB getFilm() {
		return film;
	}

	public synchronized void setFilm(FilmfromDB film) {
		this.film = film;
	}

	public synchronized List<PersonRes> getActorRes() {
		return ActorRes;
	}

	public synchronized void setActorRes(List<PersonRes> actorRes) {
		ActorRes = actorRes;
	}

	public synchronized List<PersonRes> getDirectorRes() {
		return DirectorRes;
	}

	public synchronized void setDirectorRes(List<PersonRes> directorRes) {
		DirectorRes = directorRes;
	}

	public synchronized List<MediaRes> getFilmsRes() {
		return FilmsRes;
	}

	public synchronized void setFilmsRes(List<MediaRes> filmsRes) {
		FilmsRes = filmsRes;
	}

	// prepare all the attribute of the movie in the different widgets
	public void prepareFilmPage() {
		filmName.setText(film.getName());
		film.setOriginCountry(film.getOriginCountry());
		release.setSelection(film.getYear());
		country.setText(film.getOriginCountry());
		film.prepareActorTable(actorTBL);
		film.prepareDirectorTable(directorTBL);
		value.setSelection(film.getValue());
		sequel.setText(film.getSequel());
		prequel.setText(film.getPrequel());

		// finish all ...
	}

	// prepare all the widgets listeners
	private void prepareWidgets() {

		cencel.addMouseListener(new MouseAdapter() {

			@Override
			public void mouseDown(MouseEvent arg0) {
				clearEditFilm();
				Session.gui.changeScreenTo(searchComp);
				film = null;
			}
		});
		save.addMouseListener(new MouseAdapter() {
			UpdateMediaTask updateMediaTask = null;
			LoaderGif lg;

			@SuppressWarnings("unchecked")
			@Override
			public void mouseDown(MouseEvent arg0) {

				if (updateMediaTask != null)
					Session.pool.killAll(updateMediaTask);
				if (lg == null)
					lg = new LoaderGif(loadingCNVS, 1);
				// System.out.println(film.toString());
				if (!updateFilm()) {
					Session.gui.showTextBoxWithOk("Missing film name",
							"please enter film name", SWT.ICON_WARNING);
				} else {
					cencel.setEnabled(false);
					searchActor.setEnabled(false);
					searchDirector.setEnabled(false);
					prequel.setEnabled(false);
					sequel.setEnabled(false);
					setCharacterName.setEnabled(false);
					clearEditFilm();
					updateMediaTask = new UpdateMediaTask(Session.gui
							.getDisplay(), film, lg, false, cencel,
							searchActor, searchDirector, prequel, sequel,
							setCharacterName,searchComp);
					Session.pool.pushUrgentTask(updateMediaTask,
							lg.getLoaderTask());

				}
			}
		});

		searchActor.addMouseListener(new MouseAdapter() {

			@Override
			public void mouseDown(MouseEvent arg0) {
				setVisibilty(searchActorComp, true);
				editFilm_searchActorLoadingCNVS.setVisible(false);
				isActorCompOn = true;
				cencel.setEnabled(false);
				save.setEnabled(false);
			}
		});

		searchDirector.addMouseListener(new MouseAdapter() {

			@Override
			public void mouseDown(MouseEvent arg0) {
				setVisibilty(searchDirectorComp, true);
				editFilm_searchDirectorLoadingCNVS.setVisible(false);
				isDirectorCompOn = true;
				cencel.setEnabled(false);
				save.setEnabled(false);
			}
		});

		prequel.addMouseListener(new MouseAdapter() {

			@Override
			public void mouseDown(MouseEvent arg0) {
				setVisibilty(searchFilmComp, true);
				editFilm_searchFilmLoadingCNVS.setVisible(false);
				searchFilm_filmTBL.setFocus();
				isPrequel = true;
				isFilmCompOn = true;
				cencel.setEnabled(false);
				save.setEnabled(false);
			}
		});

		sequel.addMouseListener(new MouseAdapter() {

			@Override
			public void mouseDown(MouseEvent arg0) {
				setVisibilty(searchFilmComp, true);
				editFilm_searchFilmLoadingCNVS.setVisible(false);
				searchFilm_filmTBL.setFocus();
				isPrequel = false;
				isFilmCompOn = true;
				cencel.setEnabled(false);
				save.setEnabled(false);
			}
		});

		editFilm_searchDirectorAddBTN.addMouseListener(new MouseAdapter() {

			@Override
			public void mouseDown(MouseEvent arg0) {

				if (DirectorRes != null) {
					int selection = editFilm_searchDirectorTBL
							.getSelectionIndex();
					if (selection >= 0) {
						PersonRes Director = DirectorRes.get(selection);
						film.addDirector(Director.getId(), Director.getName(),
								false);
						TableItem ti = new TableItem(directorTBL, SWT.NONE);
						ti.setText(0, Director.getName());
						setVisibilty(searchDirectorComp, false);
						editFilm_searchDirectorTBL.removeAll();
						editFilm_searchDirectorTXT.setText("");

						isDirectorCompOn = false;
						if (!isFilmCompOn && !isActorCompOn
								&& !isDirectorCompOn) {
							cencel.setEnabled(true);
							save.setEnabled(true);
						}
					}
				}
			}
		});

		editFilm_searchActorAddBTN.addMouseListener(new MouseAdapter() {

			@Override
			public void mouseDown(MouseEvent arg0) {
				if (ActorRes != null) {
					int selection = editFilm_searchActorTBL.getSelectionIndex();
					if (selection >= 0) {
						PersonRes actor = ActorRes.get(selection);
						film.addActor(actor.getId(), actor.getName(),
								editFilm_searchActorCharaterNameTXT.getText(),
								false);
						TableItem ti = new TableItem(actorTBL, SWT.NONE);
						ti.setText(0, actor.getName());
						ti.setText(1,
								editFilm_searchActorCharaterNameTXT.getText());
						setVisibilty(searchActorComp, false);
						editFilm_searchActorTBL.removeAll();
						editFilm_searchActorCharaterNameTXT.setText("");
						editFilm_searchActorTXT.setText("");

						isActorCompOn = false;

						if (!isFilmCompOn && !isActorCompOn
								&& !isDirectorCompOn) {
							cencel.setEnabled(true);
							save.setEnabled(true);
						}
					}
				}
			}
		});

		searchFilm_addBTN.addMouseListener(new MouseAdapter() {

			@Override
			public void mouseDown(MouseEvent arg0) {

				if (FilmsRes != null) {
					int selection = searchFilm_filmTBL.getSelectionIndex();
					if (selection >= 0) {
						MediaRes movie = FilmsRes.get(selection);
						if (isPrequel) {
							film.setPreqId(movie.getId());
							film.setPrequel(movie.getName());
							prequel.setText(movie.getName());
						} else {
							film.setSeqId(movie.getId());
							film.setSequel(movie.getName());
							sequel.setText(movie.getName());
						}
						setVisibilty(searchFilmComp, false);
						searchFilm_filmTBL.removeAll();
						searchFilmTXT.setText("");

						isFilmCompOn = false;
						if (!isFilmCompOn && !isActorCompOn
								&& !isDirectorCompOn) {
							cencel.setEnabled(true);
							save.setEnabled(true);
						}
					}
				}
			}
		});

		editFilm_searchActorGoBTN.addMouseListener(new MouseAdapter() {
			LoaderGif lg;

			@SuppressWarnings("unchecked")
			@Override
			public void mouseDown(MouseEvent arg0) {
				if (lg == null)
					lg = new LoaderGif(editFilm_searchActorLoadingCNVS, 1);

				if (searchActorTask != null)
					Session.pool.killAll(searchActorTask);

				if (!editFilm_searchActorTXT.getText().equals("")) {
					searchActorTask = new SearchPersonTask(Session.gui
							.getDisplay(), editFilm_searchActorTXT.getText(),
							editFilm_searchActorTBL, "actor", lg,
							editFilm_searchActorAddBTN, false);
					editFilm_searchActorTBL.removeAll();
					ActorRes = null;
					lg.showLoader();
					Session.pool.addTaskToPool(true, searchActorTask);
					Session.pool.addTaskToPool(true, lg.getLoaderTask());
				}
			}

		});

		editFilm_searchDirectorGoBTN.addMouseListener(new MouseAdapter() {
			LoaderGif lg;

			@SuppressWarnings("unchecked")
			@Override
			public void mouseDown(MouseEvent arg0) {
				if (lg == null)
					lg = new LoaderGif(editFilm_searchDirectorLoadingCNVS, 1);

				if (searchDirectorTask != null)
					Session.pool.killAll(searchDirectorTask);

				if (!editFilm_searchDirectorTXT.getText().equals("")) {
					searchDirectorTask = new SearchPersonTask(Session.gui
							.getDisplay(),
							editFilm_searchDirectorTXT.getText(),
							editFilm_searchDirectorTBL, "director", lg,
							editFilm_searchDirectorAddBTN, false);
					editFilm_searchDirectorTBL.removeAll();
					DirectorRes = null;
					lg.showLoader();
					Session.pool.addTaskToPool(true, searchDirectorTask);
					Session.pool.addTaskToPool(true, lg.getLoaderTask());
				}
			}
		});

		searchFilm_GoBTN.addMouseListener(new MouseAdapter() {
			LoaderGif lg;

			@SuppressWarnings("unchecked")
			@Override
			public void mouseDown(MouseEvent arg0) {

				if (lg == null)
					lg = new LoaderGif(editFilm_searchFilmLoadingCNVS, 1);

				if (searchFilmTask != null)
					Session.pool.killAll(searchFilmTask);

				if (!searchFilmTXT.getText().equals("")) {
					searchFilmTask = new SearchMediaTask(Session.gui
							.getDisplay(), searchFilmTXT.getText(),
							searchFilm_filmTBL, lg, searchFilm_addBTN);

					searchFilm_filmTBL.removeAll();
					FilmsRes = null;
					lg.showLoader();
					Session.pool.addTaskToPool(true, searchFilmTask);
					Session.pool.addTaskToPool(true, lg.getLoaderTask());
				}
			}
		});

		editFilm_searchActorCencelBTN.addMouseListener(new MouseAdapter() {

			@Override
			public void mouseDown(MouseEvent arg0) {
				setVisibilty(searchActorComp, false);
				editFilm_searchActorTBL.removeAll();
				editFilm_searchActorCharaterNameTXT.setText("");
				editFilm_searchActorTXT.setText("");

				Session.pool.killAll(searchActorTask);
				isActorCompOn = false;

				if (!isFilmCompOn && !isActorCompOn && !isDirectorCompOn) {
					cencel.setEnabled(true);
					save.setEnabled(true);
				}
			}

		});

		editFilm_searchDirectorCencelBTN.addMouseListener(new MouseAdapter() {

			@Override
			public void mouseDown(MouseEvent arg0) {
				setVisibilty(searchDirectorComp, false);
				editFilm_searchDirectorTXT.setText("");
				editFilm_searchDirectorTBL.removeAll();

				Session.pool.killAll(searchDirectorTask);

				isDirectorCompOn = false;
				if (!isFilmCompOn && !isActorCompOn && !isDirectorCompOn) {
					cencel.setEnabled(true);
					save.setEnabled(true);
				}
			}

		});

		searchFilm_cencelBTN.addMouseListener(new MouseAdapter() {

			@Override
			public void mouseDown(MouseEvent arg0) {
				setVisibilty(searchFilmComp, false);
				searchFilmTXT.setText("");
				searchFilm_filmTBL.removeAll();

				Session.pool.killAll(searchFilmTask);

				isFilmCompOn = false;

				if (!isFilmCompOn && !isActorCompOn && !isDirectorCompOn) {
					cencel.setEnabled(true);
					save.setEnabled(true);
				}

			}

		});
		
		setCharacterName.addMouseListener(new MouseAdapter() {

			@Override
			public void mouseDown(MouseEvent arg0) {
				int selection = actorTBL.getSelectionIndex();
				if(selection >= 0){
					actorTBL.getItem(selection).setText(1,characterName.getText());
					film.editActor(film.getActorID(selection),film.getActorName(selection), characterName.getText());
				}
			}
		});
	}

	// prepare the film object to send to the DB
	// if the name of the film is empty, we don't send it
	private boolean updateFilm() {
		if (filmName.getText().equals(""))
			return false;
		film.setName(filmName.getText());
		film.setValue(value.getSelection());
		film.setOriginCountry(country.getText());
		film.setRelease(release.getSelection(), 0, 0);
		return true;
	}

	private void clearEditFilm() {

		filmName.setText("");
		country.setText("");
		value.setSelection(0);
		actorTBL.removeAll();
		directorTBL.removeAll();
		prequel.setText("");
		sequel.setText("");
		characterName.setText("");

	}

	private void prepareSearch() {

		// prepare the search and add actor window

		editFilm_searchActorLoadingCNVS = new Canvas(searchActorComp, SWT.NONE);
		editFilm_searchActorLoadingCNVS.setVisible(false);
		editFilm_searchActorLoadingCNVS.setBounds(149, 104, 32, 32);

		editFilm_searchActorTXT = new Text(searchActorComp, SWT.BORDER
				| SWT.SEARCH);
		editFilm_searchActorTXT.setMessage("search Actor...");
		editFilm_searchActorTXT.setFont(SWTResourceManager.getFont("Segoe UI",
				16, SWT.NORMAL));
		editFilm_searchActorTXT.setText("");
		editFilm_searchActorTXT.setBounds(30, 15, 220, 40);
		editFilm_searchActorTXT.setTextLimit(300);

		editFilm_searchActorTBL = new Table(searchActorComp, SWT.BORDER
				| SWT.FULL_SELECTION);
		editFilm_searchActorTBL.setLinesVisible(true);
		editFilm_searchActorTBL.setHeaderVisible(true);
		editFilm_searchActorTBL.setBounds(30, 60, 270, 120);

		TableColumn tableColumn = new TableColumn(editFilm_searchActorTBL,
				SWT.NONE);
		tableColumn.setWidth(140);
		tableColumn.setText("Actor name");

		TableColumn tableColumn_1 = new TableColumn(editFilm_searchActorTBL,
				SWT.NONE);
		tableColumn_1.setWidth(125);
		tableColumn_1.setText("actin role");

		editFilm_searchActorGoBTN = new Button(searchActorComp, SWT.NONE);
		editFilm_searchActorGoBTN.setBounds(260, 15, 40, 40);
		editFilm_searchActorGoBTN.setText("GO!");

		editFilm_searchActorAddBTN = new Button(searchActorComp, SWT.NONE);
		editFilm_searchActorAddBTN.setBounds(220, 250, 80, 35);
		editFilm_searchActorAddBTN.setText("add actor");

		editFilm_searchActorCencelBTN = new Button(searchActorComp, SWT.NONE);
		editFilm_searchActorCencelBTN.setBounds(30, 250, 80, 35);
		editFilm_searchActorCencelBTN.setText("cencel");

		editFilm_searchActorCharaterNameTXT = new Text(searchActorComp,
				SWT.BORDER);
		editFilm_searchActorCharaterNameTXT.setFont(SWTResourceManager.getFont(
				"Segoe UI", 10, SWT.NORMAL));
		editFilm_searchActorCharaterNameTXT.setMessage("character name...");
		editFilm_searchActorCharaterNameTXT.setBounds(30, 215, 200, 25);
		editFilm_searchActorCharaterNameTXT.setTextLimit(256);

		Label editTV_searchActrorCharaterNameLBL = new Label(searchActorComp,
				SWT.NONE);
		editTV_searchActrorCharaterNameLBL.setBackground(SWTResourceManager
				.getColor(SWT.COLOR_GRAY));
		editTV_searchActrorCharaterNameLBL.setFont(SWTResourceManager.getFont(
				"Segoe UI", 11, SWT.NORMAL));
		editTV_searchActrorCharaterNameLBL.setBounds(30, 185, 135, 25);
		editTV_searchActrorCharaterNameLBL.setText("Character name:");

		// prepare the search and add Director window

		editFilm_searchDirectorLoadingCNVS = new Canvas(searchDirectorComp,
				SWT.NONE);
		editFilm_searchDirectorLoadingCNVS.setVisible(false);
		editFilm_searchDirectorLoadingCNVS.setBounds(149, 104, 32, 32);

		editFilm_searchDirectorTXT = new Text(searchDirectorComp, SWT.BORDER
				| SWT.SEARCH);
		editFilm_searchDirectorTXT.setMessage("search Director...");
		editFilm_searchDirectorTXT.setText("");
		editFilm_searchDirectorTXT.setFont(SWTResourceManager.getFont(
				"Segoe UI", 16, SWT.NORMAL));
		editFilm_searchDirectorTXT.setBounds(30, 15, 212, 37);
		editFilm_searchDirectorTXT.setTextLimit(300);

		editFilm_searchDirectorGoBTN = new Button(searchDirectorComp, SWT.NONE);
		editFilm_searchDirectorGoBTN.setText("GO!");
		editFilm_searchDirectorGoBTN.setBounds(260, 15, 40, 40);

		editFilm_searchDirectorTBL = new Table(searchDirectorComp, SWT.BORDER
				| SWT.FULL_SELECTION);
		editFilm_searchDirectorTBL.setLinesVisible(true);
		editFilm_searchDirectorTBL.setHeaderVisible(true);
		editFilm_searchDirectorTBL.setBounds(30, 60, 270, 100);

		TableColumn tblclmnCreatorName_1;
		tblclmnCreatorName_1 = new TableColumn(editFilm_searchDirectorTBL,
				SWT.NONE);
		tblclmnCreatorName_1.setWidth(148);
		tblclmnCreatorName_1.setText("Director name");

		TableColumn tableColumn_3 = new TableColumn(editFilm_searchDirectorTBL,
				SWT.NONE);
		tableColumn_3.setWidth(115);
		tableColumn_3.setText("known for");

		editFilm_searchDirectorCencelBTN = new Button(searchDirectorComp,
				SWT.NONE);
		editFilm_searchDirectorCencelBTN.setText("cencel");
		editFilm_searchDirectorCencelBTN.setBounds(30, 170, 80, 35);

		editFilm_searchDirectorAddBTN = new Button(searchDirectorComp, SWT.NONE);
		editFilm_searchDirectorAddBTN.setText("add Director");
		editFilm_searchDirectorAddBTN.setBounds(210, 170, 90, 35);

		// prepare the film search...

		editFilm_searchFilmLoadingCNVS = new Canvas(searchFilmComp, SWT.NONE);
		editFilm_searchFilmLoadingCNVS.setVisible(false);
		editFilm_searchFilmLoadingCNVS.setBounds(119, 92, 32, 32);

		searchFilmTXT = new Text(searchFilmComp, SWT.BORDER);
		searchFilmTXT.setText("");
		searchFilmTXT.setMessage("search Film");
		searchFilmTXT.setFont(SWTResourceManager.getFont("Segoe UI", 14,
				SWT.NORMAL));
		searchFilmTXT.setBounds(10, 10, 205, 40);
		searchFilmTXT.setTextLimit(200);

		searchFilm_GoBTN = new Button(searchFilmComp, SWT.NONE);
		searchFilm_GoBTN.setText("GO!");
		searchFilm_GoBTN.setBounds(220, 10, 40, 40);

		searchFilm_filmTBL = new Table(searchFilmComp, SWT.BORDER
				| SWT.FULL_SELECTION);
		searchFilm_filmTBL.setLinesVisible(true);
		searchFilm_filmTBL.setHeaderVisible(true);
		searchFilm_filmTBL.setBounds(10, 53, 250, 110);

		TableColumn tblclmnFilm = new TableColumn(searchFilm_filmTBL, SWT.NONE);
		tblclmnFilm.setWidth(120);
		tblclmnFilm.setText("Film");

		TableColumn tblclmnReleaseYear = new TableColumn(searchFilm_filmTBL,
				SWT.NONE);
		tblclmnReleaseYear.setWidth(120);
		tblclmnReleaseYear.setText("release year");

		searchFilm_cencelBTN = new Button(searchFilmComp, SWT.NONE);
		searchFilm_cencelBTN.setText("cencel");
		searchFilm_cencelBTN.setBounds(10, 170, 80, 40);

		searchFilm_addBTN = new Button(searchFilmComp, SWT.NONE);
		searchFilm_addBTN.setText("add Film");
		searchFilm_addBTN.setBounds(170, 170, 90, 40);

		setVisibilty(searchDirectorComp, false);
		setVisibilty(searchFilmComp, false);
		setVisibilty(searchActorComp, false);

	}

	private void setVisibilty(Composite composite, boolean visi) {

		for (Control c : composite.getChildren()) {
			c.setVisible(visi);
		}
		composite.setVisible(visi);

	}

}
